package com.zu.commu.interf.gprs;

import com.zu.commu.exception.CommuException;

import java.util.regex.Pattern;

public class ProcessOnLine
{
  public static Pattern pCongXing = Pattern.compile(
    "[\\x00\\x01\\x02\\x05\\x09\\x0A].{9}.{2}.*", 32);

  public static Pattern pLanDi = Pattern.compile("@@\\d{11}IP:.*", 
    32);

  public static Pattern pSanLong = Pattern.compile("@ID.*", 
    32);

  public static Pattern pGT08 = Pattern.compile("GT08.{2}.*");

  public static Pattern pAmGprs1_0 = Pattern.compile(
    "\\xAA.{2}\\xAA.*", 32);

  public static Pattern pAmGprs1_5 = Pattern.compile(
    "\\xAA.{2}\\xAA.*", 32);

  public static Pattern pAmGprs2_0 = Pattern.compile(
    "\\xAA.{2}\\xAA.*", 32);

  public String[] getGPRS_ID_IP(byte[] bs)
    throws Exception
  {
    String s = new String(bs, "ISO-8859-1").trim();
    String[] ss = new String[2];
    try
    {
      if (pCongXing.matcher(s).matches())
      {
        ss[0] = s.substring(1, 10).trim();
        ss[1] = null;
      } else if (pGT08.matcher(s).matches())
      {
        int value = (bs[6] + 256) % 256 * 256 + (bs[7] + 256) % 256;
        ss[0] = String.valueOf(value);
        ss[1] = null;
      } else if (pSanLong.matcher(s).matches()) {
        ss[0] = s.substring(3, 14);
        ss[1] = null;
      } else if (pLanDi.matcher(s).matches())
      {
        ss[0] = s.substring(2, s.indexOf("IP:"));
        ss[1] = s.substring(s.indexOf("IP:") + 3, s.indexOf("##"));
      }
      else if (pAmGprs1_0.matcher(s).matches())
      {
        ss[0] = createGPRSID(BCD2String(bs, 4, 9));

        ss[1] = null;
      } else if (pAmGprs1_5.matcher(s).matches())
      {
        ss[0] = s.substring(5, 11).trim();
        ss[1] = null;
      } else if (pAmGprs2_0.matcher(s).matches())
      {
        ss[0] = s.substring(5, 11).trim();
        ss[1] = null;
      } else {
        throw new Exception("未知的GPRS协议");
      }
    }
    catch (Exception e) {
      throw e;
    }
    return ss;
  }

  public boolean isGPRSID(String gprsId, int len)
  {
    return true;
  }

  public boolean isIntNumber(String str)
  {
    for (int i = 0; i < str.length(); i++) {
      if (new String("9876543210").indexOf(str.substring(i, i + 1)) == -1) {
        return false;
      }
    }
    return true;
  }

  private String BCD2String(byte[] b, int startIndex, int endIndex)
    throws CommuException
  {
    String str = decodeBCD(b, startIndex, endIndex - startIndex + 1);
    return cleanHead0FromStrNum(str);
  }

  private String decodeBCD(byte[] src, int startIndex, int length)
  {
    StringBuilder sb = new StringBuilder();
    for (int i = startIndex; i < startIndex + length; i++) {
      int value = (src[i] + 256) % 256;
      sb.append((char)(value / 16 + 48)).append(
        (char)(value % 16 + 48));
      value++;
    }
    String result = sb.toString();
    if (!result.matches("\\d*")) {
      throw new IllegalArgumentException();
    }
    return result;
  }

  private String createGPRSID(String result)
  {
    String result2 = "";
    if (result.length() == 12) {
      for (int i = 1; i < result.length(); i += 2) {
        result2 = result2 + result.substring(i, i + 1);
        result2 = result2 + result.substring(i - 1, i);
      }
      result2 = cleanTailOne0FromStrNum(result2);
    }
    return result2;
  }

  private String cleanTailOne0FromStrNum(String s)
  {
    int i = s.length() - 1;

    if (s.charAt(i) == '0') {
      s = s.substring(0, i);
    }
    return s;
  }

  private String cleanHead0FromStrNum(String s)
  {
    int i = 0;
    while ((i < s.length()) && (s.charAt(i) == '0')) {
      i++;
    }
    if (i > 0) {
      s = s.substring(i);
    }
    return s;
  }

  private String getGPRSIP(byte[] b)
  {
    int ip0 = byte2Int(b[13]);
    int ip1 = byte2Int(b[14]);
    int ip2 = byte2Int(b[15]);
    int ip3 = byte2Int(b[16]);
    int ip4 = byte2Int(b[17]);
    int ip5 = byte2Int(b[18]);
    int ip6 = byte2Int(b[19]);
    int ip7 = byte2Int(b[20]);
    int ip8 = byte2Int(b[21]);
    int ip9 = byte2Int(b[22]);
    int ip10 = byte2Int(b[23]);
    int ip11 = byte2Int(b[24]);
    int ip12 = byte2Int(b[25]);
    int ip13 = byte2Int(b[26]);
    int ip14 = byte2Int(b[27]);
    int ip15 = byte2Int(b[28]);
    String ip = "";
    ip = ip + ip0 + ".";
    ip = ip + ip1 + ".";
    ip = ip + ip2 + ".";
    ip = ip + ip3 + ".";
    ip = ip + ip4 + ".";
    ip = ip + ip5 + ".";
    ip = ip + ip6 + ".";
    ip = ip + ip7 + ".";
    ip = ip + ip8 + ".";
    ip = ip + ip9 + ".";
    ip = ip + ip10 + ".";
    ip = ip + ip11 + ".";
    ip = ip + ip12 + ".";
    ip = ip + ip13 + ".";
    ip = ip + ip14 + ".";
    ip = ip + ip15;

    return ip;
  }

  private int byte2Int(byte b)
  {
    return (b + 256) % 256;
  }
}